﻿using Foxit.PDF;
using Foxit.PDF.IO;
using Foxit.PDF.PageElements;
using System.Globalization;

namespace Sustainalytics.Reporting.Framework
{
    public class RelativePositionBullet : PageElement
    {
        public float Left { get; set; }
        public float Top { get; set; }
        public float Radius { get; set; }
        public int Rank { get; set; }
        public int Count { get; set; }
        public Font ValueFont { get; set; }
        public float ValueFontSize { get; set; }

        private const string NaN = "N/A";
        private const int NoValue = -1;

        private Group Elements()
        {
            const float borderWidth = 0.1f;

            var elements = new Group();
            
            elements.Add(
                new Circle(Left + Radius, Top + Radius, Radius, Colors.YellowBulletColor, Colors.YellowBulletColor, borderWidth, LineStyle.Solid));

            var labelWidth = Radius * 2;
            var lebelHeigth = ValueFontSize + 2f;
            var labelTopOffset = (Radius * 2 - ValueFontSize) / 2 + 1f;

            var valueColor = RgbColor.White;          

            if (Rank <= NoValue)
            {
                var valueFontSize = ValueFontSize - 4f;

                elements.Add(new Label(NaN,
                    Left, Top + labelTopOffset, labelWidth, lebelHeigth, ValueFont, valueFontSize, TextAlign.Center,
                    valueColor));
            }
            else
            {
                elements.Add(
                 new Label(Rank.ToString(CultureInfo.InvariantCulture),
                     Left, Top + 4f, 35f, ValueFontSize, ValueFont, ValueFontSize, TextAlign.Center, valueColor));
                elements.Add(
                    new Label("out of",
                        Left, Top + 14, 35f, 10f, ValueFont, 8, TextAlign.Center, valueColor));
                elements.Add(
                    new Label(Count.ToString(CultureInfo.InvariantCulture),
                        Left, Top + 21f, 35f, ValueFontSize, ValueFont, ValueFontSize, TextAlign.Center, valueColor));
            }

            return elements;
        }

        public override void Draw(PageWriter writer)
        {
            var elements = Elements();

            writer.Write_q_(true);
            elements.Draw(writer);
            writer.Write_Q(true);
        }
    }
}
